﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsWindow.xaml.cs" company="Geologische Bundesanstalt">
//   Thomas Heuberger, 2014-02
// </copyright>
// <summary>
//   Interaction logic for SettingsWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AttachmentHandler {
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    using Outlook = Microsoft.Office.Interop.Outlook;

    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window {
        /// <summary>
        /// The errors.
        /// </summary>
        private int errors = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsWindow"/> class.
        /// </summary>
        public SettingsWindow() {
            this.InitializeComponent();
            this.DataContext = this;

            // init the settings instance with the values from global addin settings instance
            this.Settings = Globals.ThisAddIn.Settings;
        }

        /// <summary>
        /// Gets or sets the settings.
        /// </summary>
        public Settings Settings { get; set; }

        /// <summary>
        /// The button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void SourceButtonClick(object sender, RoutedEventArgs e) {
           this.Settings.SourceFolder = Globals.ThisAddIn.Application.Session.PickFolder() as Outlook.Folder;
        }

        /// <summary>
        /// The destination button click handler.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DestinationButtonClick(object sender, RoutedEventArgs e) {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            var result = dialog.ShowDialog();
            if (result != System.Windows.Forms.DialogResult.OK) return;
            
            this.Settings.DestinationFolder = dialog.SelectedPath;
        }

        /// <summary>
        /// The confirm can execute method.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ConfirmCanExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = this.errors == 0;
            e.Handled = true;
        }

        /// <summary>
        /// The confirm executed method.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ConfirmExecuted(object sender, ExecutedRoutedEventArgs e) {
            // save the settings ...
            Globals.ThisAddIn.Settings.Save();

            this.Close();
        }

        /// <summary>
        /// The validation error method.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ValidationError(object sender, ValidationErrorEventArgs e) {
            if (e.Action == ValidationErrorEventAction.Added)
                this.errors++;
            else
                this.errors--;
        }

        /// <summary>
        /// The button_ mouse right button up event handler.
        /// Restores the settings to defaults. Used for debugging only!
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void ButtonMouseRightButtonUp(object sender, MouseButtonEventArgs e) {
            if (Keyboard.IsKeyDown(Key.LeftShift)) {
                Globals.ThisAddIn.Settings.RestoreDefault();
            }
        }
    }
}
